home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample PMSAM / PMSAM Framework / RoboSamSlot / IncomingHFSStream.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  9.5 KB  |  589 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        IncomingHFSStream.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    David Akhond
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.  
  13.     To Do: 
  14.     
  15. */
  16.  
  17.  
  18. #ifndef __IncomingHFSStream__
  19. #include    "IncomingHFSStream.h"
  20. #endif
  21.  
  22.  
  23.  
  24. #pragma segment HFSSlot
  25.  
  26. //---------------------
  27. //    constructor
  28. //---------------------
  29.  
  30.     CIncomingHFSStream::CIncomingHFSStream(CFile*            theLetterFile)
  31. {
  32.  
  33.     fLetterFile = theLetterFile;
  34.     
  35.     fLetterPos = eNone;
  36.     fStreamPos = 0;
  37.     fLineBufLen = 0;
  38.     fLineBufPos = 0;
  39.     
  40.     OSErr    err = fLetterFile->Open(fsRdWrPerm);
  41.     
  42. }
  43.  
  44.  
  45.  
  46. //-------------------
  47. //    destructor
  48. //-------------------
  49.  
  50. CIncomingHFSStream::~CIncomingHFSStream()
  51. {
  52.  
  53.     OSErr    err = fLetterFile->Close();
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. //----------------------
  61. //    Recipients
  62. //----------------------
  63.  
  64. void                CIncomingHFSStream::FromRecipient(char*    author)
  65. {
  66.     long             newPosition = 0;
  67.  
  68.     fStreamPos = newPosition;
  69.  
  70.     
  71.         fLetterFile->SetPosition( fStreamPos );
  72.         
  73.         this->FromField();
  74.  
  75.         strcpy(author, fFromRecipient);
  76.         
  77. return;
  78. }
  79.  
  80.  
  81. //----------------------------------------
  82. Boolean                CIncomingHFSStream::NextToRecipient(char*    nextToRecipient)
  83. {
  84.     long        dataLen = 63;
  85.     char         commaSep = ',';
  86.     char        carReturn = 0x0D;
  87.     char        tempChar = 0x00;
  88.     long        tempRecipOffset = 0;
  89.     Boolean        more = true;
  90.     Boolean        toRecipComplete = false;
  91.     
  92.     memset(nextToRecipient, 0, 64);
  93.     
  94.     if (fLetterPos != eToRecips)
  95.     {
  96.         this->ToField();
  97.     }
  98.     
  99.     if(fLetterPos == eToRecips)
  100.     {
  101.         while( !toRecipComplete )
  102.         {
  103.             if( tempRecipOffset > 62 )
  104.             {
  105.                 toRecipComplete = true;
  106.             }
  107.                 else
  108.                 {
  109.                     tempChar = fLineBuf[fLineBufPos];
  110.                     fLineBufPos++;
  111.                     
  112.                     if( (tempChar == ',') || (tempChar == 0x0D) )
  113.                     {
  114.                         toRecipComplete = true;
  115.                         nextToRecipient[tempRecipOffset] = 0x00;
  116.                     }
  117.                       else
  118.                       {
  119.                          nextToRecipient[tempRecipOffset] = tempChar;
  120.                          tempRecipOffset++;
  121.                       }
  122.                       
  123.                     if( tempChar == 0x0D )
  124.                     {
  125.                         more = false;
  126.                     }
  127.                 }
  128.         }
  129.     }
  130.         else
  131.         {
  132.             more = false;
  133.         }
  134.  
  135. return(more);    
  136. }
  137.  
  138.  
  139. //----------------------------------------
  140. Boolean                CIncomingHFSStream::NextCCRecipient(char*    nextCCRecipient)
  141. {
  142.     long        dataLen = 63;
  143.     char         commaSep = ',';
  144.     char        carReturn = 0x0D;
  145.     char        tempChar = 0x00;
  146.     long        tempRecipOffset = 0;
  147.     Boolean        more = true;
  148.     Boolean        ccRecipComplete = false;
  149.     
  150.     memset(nextCCRecipient, 0, 64);
  151.     
  152.     if (fLetterPos != eCCRecips)
  153.     {
  154.         this->CCField();
  155.     }
  156.     
  157.     if(fLetterPos == eCCRecips)
  158.     {
  159.         while( !ccRecipComplete )
  160.         {
  161.             if( tempRecipOffset > 62 )
  162.             {
  163.                 ccRecipComplete = true;
  164.             }
  165.                 else
  166.                 {
  167.                     tempChar = fLineBuf[fLineBufPos];
  168.                     fLineBufPos++;
  169.                     
  170.                     if( (tempChar == ',') || (tempChar == 0x0D) )
  171.                     {
  172.                         ccRecipComplete = true;
  173.                         nextCCRecipient[tempRecipOffset] = 0x00;
  174.                     }
  175.                       else
  176.                       {
  177.                          nextCCRecipient[tempRecipOffset] = tempChar;
  178.                          tempRecipOffset++;
  179.                       }
  180.                       
  181.                     if( tempChar == 0x0D )
  182.                     {
  183.                         more = false;
  184.                     }
  185.                 }
  186.         }
  187.     }
  188.         else
  189.         {
  190.             more = false;
  191.         }
  192.  
  193. return(more);    
  194. }
  195.  
  196.  
  197.  
  198. //----------------------------------------
  199. Boolean                CIncomingHFSStream::NextBCCRecipient(char*    nextBCCRecipient)
  200. {
  201.     long        dataLen = 63;
  202.     char         commaSep = ',';
  203.     char        carReturn = 0x0D;
  204.     char        tempChar = 0x00;
  205.     long        tempRecipOffset = 0;
  206.     Boolean        more = true;
  207.     Boolean        bccRecipComplete = false;
  208.     
  209.     memset(nextBCCRecipient, 0, 64);
  210.     
  211.     if (fLetterPos != eBccRecips)
  212.     {
  213.         this->BCCField();
  214.     }
  215.     
  216.     if(fLetterPos == eBccRecips)
  217.     {
  218.         while( !bccRecipComplete )
  219.         {
  220.             if( tempRecipOffset > 62 )
  221.             {
  222.                 bccRecipComplete = true;
  223.             }
  224.                 else
  225.                 {
  226.                     tempChar = fLineBuf[fLineBufPos];
  227.                     fLineBufPos++;
  228.                     
  229.                     if( (tempChar == ',') || (tempChar == 0x0D) )
  230.                     {
  231.                         bccRecipComplete = true;
  232.                         nextBCCRecipient[tempRecipOffset] = 0x00;
  233.                     }
  234.                       else
  235.                       {
  236.                          nextBCCRecipient[tempRecipOffset] = tempChar;
  237.                          tempRecipOffset++;
  238.                       }
  239.                       
  240.                     if( tempChar == 0x0D )
  241.                     {
  242.                         more = false;
  243.                     }
  244.                 }
  245.         }
  246.     }
  247.         else
  248.         {
  249.             more = false;
  250.         }
  251.  
  252. return(more);    
  253. }
  254.  
  255.  
  256.  
  257.  
  258. //------------------------------------------
  259. Boolean                CIncomingHFSStream::BodyText( char*    textBuff, long*    count )
  260. {
  261.     long        maxSize = *count;
  262.     long        amountRead = 0;
  263.     char        tempChar = 0x00;
  264.     Boolean        more = true;    
  265.     long        lenFileRemaining;
  266.     long        filePosCurr;
  267.     long        fileSizeCurr;
  268.  
  269.     if (fLetterPos != eBody)
  270.     {
  271.         this->BodyField();
  272.     }
  273.     
  274.     if(fLetterPos == eBody)
  275.     {
  276.     
  277.         fileSizeCurr = fLetterFile->GetSize();
  278.         filePosCurr = fLetterFile->GetPosition();
  279.         lenFileRemaining = fileSizeCurr - filePosCurr;
  280.         
  281.         if( lenFileRemaining <= maxSize )
  282.         {
  283.             more = false;
  284.             maxSize = lenFileRemaining;
  285.         }
  286.         
  287.         
  288.         for( long x = 0; x < maxSize; x++)
  289.         {
  290.             
  291.             *(textBuff+x) = fLetterFile->ReadByte();
  292.             amountRead++;
  293.             fStreamPos++;
  294.             fLetterFile->SetPosition(fStreamPos);
  295.         }
  296.         
  297.         *count = amountRead;
  298.  
  299.     }
  300.         else
  301.         {
  302.             *count = 0;
  303.             more = false;
  304.         }
  305.  
  306. return(more);
  307. }
  308.  
  309.  
  310.  
  311.  
  312. //----------------------------
  313. //    FromField
  314. //----------------------------
  315.  
  316. void        CIncomingHFSStream::FromField()
  317. {
  318.     long        charCount = 0;
  319.     char         lineBuf[kMaxLineLen];
  320.     long        maxSize = kMaxLineLen;
  321.     char         lineTerminator = 0x0D;
  322.     Boolean        fromFound = false;
  323.     short        fromRecipPos = 0;
  324.     
  325.     fLetterPos = eNone;
  326.     memset(fFromRecipient, 0, 64);
  327.     fStreamPos = 0;
  328.     fLetterFile->SetPosition(fStreamPos);
  329.  
  330.     long    eofMark = fLetterFile->GetSize();    
  331.  
  332.     while( !fromFound  )
  333.     {
  334.     
  335.         charCount = fLetterFile->ReadLine (lineBuf,maxSize, lineTerminator);
  336.         
  337.         fStreamPos += charCount;
  338.         
  339.         fLetterFile->SetPosition(fStreamPos);
  340.         
  341.         if(charCount > 5)
  342.         {
  343.             if( (lineBuf[0] == 'F') && (lineBuf[1] == 'r') && (lineBuf[2] == 'o') && (lineBuf[3] == 'm') && (lineBuf[4] == ':') )
  344.             {
  345.                 // we are on the 'From:' line
  346.                 
  347.                 if((charCount-5) > 1)
  348.                 {
  349.                     long x = 5;
  350.                     
  351.                     while( x < charCount )
  352.                     {
  353.                         char tempChar = *(lineBuf + x);
  354.                         
  355.                         if(tempChar == 0x0D)
  356.                         {
  357.                             break;
  358.                         }
  359.                             else
  360.                             {
  361.                                 fFromRecipient[fromRecipPos] = tempChar;
  362.                                 fromRecipPos++;
  363.                             }
  364.                         
  365.                         x++;
  366.                     }
  367.                     fFromRecipient[fromRecipPos] = 0x00;
  368.                     fromFound = true;
  369.                     fLetterPos = eFromRecips;
  370.                 }
  371.                     else
  372.                     {
  373.                         // set a default From recipient since there was no data
  374.                         strcpy (fFromRecipient, "defaultFrom");
  375.                     }
  376.             }
  377.         }
  378.         
  379.         if(fStreamPos == eofMark)
  380.         {
  381.             // if we're at the end of the file, then use the default From address
  382.             strcpy (fFromRecipient, "defaultFrom");
  383.             fromFound = true;
  384.         }
  385.         
  386.     }
  387.  
  388. }
  389.  
  390.  
  391.  
  392.  
  393. //----------------------------
  394. //    ToField
  395. //----------------------------
  396.  
  397. void        CIncomingHFSStream::ToField()
  398. {
  399.     long        charCount = 0;
  400.     Boolean        toFound = false;
  401.     char         lineBuf[kMaxLineLen];
  402.     long        maxSize = kMaxLineLen;
  403.     char         lineTerminator = 0x0D;
  404.  
  405.     fLetterPos = eNone;
  406.     fStreamPos = 0;
  407.     fLetterFile->SetPosition(fStreamPos);
  408.     long    eofMark = fLetterFile->GetSize();
  409.     
  410.     while( !toFound )
  411.     {
  412.     
  413.         charCount = fLetterFile->ReadLine(fLineBuf,maxSize, lineTerminator);
  414.         fLineBufLen = charCount;
  415.         
  416.         
  417.         fStreamPos += charCount;
  418.         
  419.         fLetterFile->SetPosition(fStreamPos);
  420.         
  421.         if( (fLineBuf[0] == 'T') && (fLineBuf[1] == 'o') && (fLineBuf[2] == ':') )
  422.         {
  423.             long x=3;
  424.             fLineBufPos = x;
  425.             fLetterPos = eToRecips;
  426.             toFound = true;
  427.         }
  428.         
  429.         if( fStreamPos == eofMark )
  430.         {
  431.             break;
  432.         }
  433.     
  434.     }
  435.  
  436.  
  437. }
  438.  
  439.  
  440. //----------------------------
  441. //    CCField
  442. //----------------------------
  443.  
  444. void        CIncomingHFSStream::CCField()
  445. {
  446.     long        charCount = 0;
  447.     Boolean        ccFound = false;
  448.     char         lineBuf[kMaxLineLen];
  449.     long        maxSize = kMaxLineLen;
  450.     char         lineTerminator = 0x0D;
  451.     char        currChar = 0x00;
  452.  
  453.     fLetterPos = eNone;
  454.     fStreamPos = 0;
  455.     fLetterFile->SetPosition(fStreamPos);
  456.     long    eofMark = fLetterFile->GetSize();
  457.  
  458.     while( !ccFound  )
  459.     {
  460.     
  461.         charCount = fLetterFile->ReadLine(fLineBuf,maxSize, lineTerminator);
  462.         fLineBufLen = charCount;
  463.         
  464.         
  465.         fStreamPos += charCount;
  466.         
  467.         fLetterFile->SetPosition(fStreamPos);
  468.         
  469.         if( (fLineBuf[0] == 'C') && (fLineBuf[1] == 'C') && (fLineBuf[2] == ':') )
  470.         {
  471.             long x=3;
  472.             fLineBufPos = x;
  473.             fLetterPos = eCCRecips;
  474.             ccFound = true;
  475.         }
  476.         
  477.         if( fStreamPos == eofMark )
  478.         {
  479.             break;
  480.         }
  481.     
  482.     }
  483.  
  484.  
  485. }
  486.  
  487.  
  488.  
  489. //----------------------------
  490. //    BCCField
  491. //----------------------------
  492.  
  493. void        CIncomingHFSStream::BCCField()
  494. {
  495.     long        charCount = 0;
  496.     Boolean        bccFound = false;
  497.     char         lineBuf[kMaxLineLen];
  498.     long        maxSize = kMaxLineLen;
  499.     char         lineTerminator = 0x0D;
  500.     char        currChar = 0x00;
  501.  
  502.     fLetterPos = eNone;
  503.     fStreamPos = 0;
  504.     fLetterFile->SetPosition(fStreamPos);
  505.     long    eofMark = fLetterFile->GetSize();
  506.  
  507.     while( !bccFound )
  508.     {
  509.     
  510.         charCount = fLetterFile->ReadLine(fLineBuf,maxSize, lineTerminator);
  511.         fLineBufLen = charCount;
  512.         
  513.         
  514.         fStreamPos += charCount;
  515.         
  516.         fLetterFile->SetPosition(fStreamPos);
  517.         
  518.         if( (fLineBuf[0] == 'B') && (fLineBuf[1] == 'C') && (fLineBuf[2] == 'C') && (fLineBuf[3] == ':') )
  519.         {
  520.             long x=4;
  521.             fLineBufPos = x;
  522.             fLetterPos = eBccRecips;
  523.             bccFound = true;
  524.         }
  525.         
  526.         if( fStreamPos == eofMark )
  527.         {
  528.             break;
  529.         }
  530.     
  531.     }
  532.  
  533.  
  534. }
  535.  
  536.  
  537.  
  538. //----------------------------
  539. //    BodyField
  540. //----------------------------
  541.  
  542. void        CIncomingHFSStream::BodyField()
  543. {
  544.     long        charCount = 0;
  545.     Boolean        bodyFound = false;
  546.     char         lineBuf[kMaxLineLen];
  547.     long        maxSize = kMaxLineLen;
  548.     char         lineTerminator = 0x0D;
  549.     char        currChar = 0x00;
  550.  
  551.     fLetterPos = eNone;
  552.     fStreamPos = 0;
  553.     fLetterFile->SetPosition(fStreamPos);
  554.     long    eofMark = fLetterFile->GetSize();
  555.  
  556.     while( !bodyFound )
  557.     {
  558.     
  559.         charCount = fLetterFile->ReadLine(fLineBuf,maxSize, lineTerminator);
  560.         fLineBufLen = charCount;
  561.         
  562.         
  563.         fStreamPos += charCount;
  564.         
  565.         fLetterFile->SetPosition(fStreamPos);
  566.         
  567.         if( (fLineBuf[0] == 'B') && (fLineBuf[1] == 'o') && (fLineBuf[2] == 'd') && (fLineBuf[3] == 'y') && (fLineBuf[4] == ':') )
  568.         {
  569.             long x=5;
  570.             fLineBufPos = x;
  571.             fLetterPos = eBody;
  572.             bodyFound = true;
  573.         }
  574.         
  575.         if( fStreamPos == eofMark )
  576.         {
  577.             break;
  578.         }
  579.     
  580.     }
  581.  
  582.  
  583. }
  584.  
  585.  
  586.  
  587.  
  588.  
  589.